/*
 * @lc app=leetcode.cn id=957 lang=javascript
 *
 * [957] N 天后的牢房
 */

// @lc code=start
/**
 * @param {number[]} cells
 * @param {number} N
 * @return {number[]}
 */
var prisonAfterNDays = function(cells, N) {
    var result = new Array(cells.length), length = cells.length, T = 0, startCells = '', count = 0;
    for(let i=0;i<length-1;i++){
        if(cells[i-1]===cells[i+1]) result[i]=1;
        else result[i] = 0;
    } 
    N--; //从一天后开始 保证首尾都是0 找周期T
    startCells = result.join(''); //备份参考字符串
    cells = result.join().split(',');
    while(N>0){
        for(let i=1;i<length-2;i++){
            if(cells[i-1]===cells[i+1]) result[i]=1;
            else result[i] = 0;
        }
        count++;
        if(result.join('')===startCells){
            N = N%count;
        }
        cells = result.join().split(',');
    }
    return cells;
};
// @lc code=end

